package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.*;
import cn.wolfcode.luowowo.article.mapper.StrategyContentMapper;
import cn.wolfcode.luowowo.article.mapper.StrategyDetailMapper;
import cn.wolfcode.luowowo.article.mapper.StrategyMapper;
import cn.wolfcode.luowowo.article.mapper.StrategyTagMapper;
import cn.wolfcode.luowowo.article.query.StrategyDetailQuery;
import cn.wolfcode.luowowo.article.query.TravelQuery;
import cn.wolfcode.luowowo.article.service.*;
import cn.wolfcode.luowowo.article.vo.ArticleStrategyStatisVO;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

@Service
public class StrategyDetailServiceImpl implements IStrategyDetailService {

    @Autowired
    private StrategyDetailMapper strategyDetailMapper;

    @Autowired
    private StrategyContentMapper strategyContentMapper;

    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private IDestinationService destinationService;

    @Autowired
    private IStrategyTagService strategyTagService;
    @Autowired
    private StrategyTagMapper strategyTagMapper;

    @Override
    public PageInfo query(StrategyDetailQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        return new PageInfo(strategyDetailMapper.selectForList(qo));
    }
    @Override
    public List<StrategyDetail> list() {
        return strategyDetailMapper.selectAll();
    }

    @Override
    public StrategyDetail get(Long id) {
        return strategyDetailMapper.selectByPrimaryKey(id);
    }

    @Override
    public void saveOrUpdate(StrategyDetail strategyDetail, String tags) {

        //操作前准备
        //1.目的地id
        Strategy strategy = strategyService.get(strategyDetail.getStrategy().getId());
        Long destId = strategy.getDest().getId();
        strategyDetail.setDest(strategy.getDest());
        //2.作者id

        //3.简介
        //截取内容信息的前200个字,不超过则直接设置
        String content = strategyDetail.getStrategyContent().getContent();
        if(content != null){
            if(StringUtils.length(content) > 200){
                strategyDetail.setSummary(content.substring(0,200));
            }else {
                strategyDetail.setSummary(content);
            }
        }
        //5.是否国外
        strategyDetail.setIsabroad(destId !=1);

        //6.打破关系
        strategyDetailMapper.deleteRelation(strategyDetail.getId());



        if(strategyDetail.getId() == null){
            //创建时间
            strategyDetail.setCreateTime(new Date());
            strategyDetailMapper.insert(strategyDetail);

        }else {
            strategyDetailMapper.updateByPrimaryKey(strategyDetail);
        }

        //7.维护关系
        String[] strings = tags.split(",");
        if(strings.length!=0) {
            for (String tag : strings) {
                StrategyTag strategyTag = strategyTagMapper.selectByTag(tag);
                if (strategyTag == null) {
                    StrategyTag tag1 = new StrategyTag();
                    tag1.setName(tag);
                    strategyTagMapper.insert(tag1);
                    strategyDetailMapper.insertRelation(strategyDetail.getId(),tag1.getId());
                }else {
                    strategyDetailMapper.insertRelation(strategyDetail.getId(),strategyTag.getId());
                }
            }
        }
    }

    @Override
    public StrategyContent getContent(Long id) {
        return strategyDetailMapper.selectContentById(id);
    }


    @Override
    public List<StrategyDetail> queryViewNumTop3(Long id) {
        return strategyDetailMapper.queryViewNumTop3(id);
    }

    @Override
    public String queryTagsByDetailId(Long id) {
        return strategyDetailMapper.queryTagsByDetailId(id);
    }

    @Override
    public int addReplynum(Long detailId) {
        return strategyDetailMapper.addReplynum(detailId);
    }

    @Override
    public void persistenceDate(ArticleStrategyStatisVO avo) {
        StrategyDetail detail = new StrategyDetail();
        BeanUtils.copyProperties(avo,detail);
        detail.setId(avo.getStrategyId());
        strategyDetailMapper.updateRedisDate(detail);
    }

    @Override
    public List<StrategyDetail> getDestVoByUserId(Long id) {
        List<StrategyDetail> list =  strategyDetailMapper.getDestVoByUserId(id);
        return list;
    }

}
